// pages/my/my.js
const App = getApp();
const HTTP = require('../../utils/util');
const imageUrl = '../../images/my_icon/';
const common = require('../../libs/common');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        iconsList: [
            {
                pic: imageUrl + 'icon_dingdan.png',
                name: '我的订单',
                url: '../order/order',
                id: 1
            },
            {
                pic: imageUrl + 'icon_pingjia.png',
                name: '我的评价',
                url: '../myremark/myremark',
                id: 2
            },
            {
                pic: imageUrl + 'icon_tuandui.png',
                name: '我的团队',
                url: '../mymembers/mymembers',
                id: 3
            },
            {
                pic: imageUrl + 'icon_dianzan.png',
                name: '我的赞',
                url: '../mythumbs/mythumbs',
                id: 4
            },
            {
                pic: imageUrl + 'icon_yaoqingma.png',
                name: '我的邀请码',
                url: '../myinvitecode/myinvitecode',
                id: 5
            },
            {
                pic: imageUrl + 'icon_shangjia.png',
                name: '申请成为商家',
                url: '../tobeboss/tobeboss',
                id: 6
            },
            {
                pic: imageUrl + 'icon_shoukuanma.png',
                name: '我的收款码',
                url: '../myqrcode/myqrcode',
                id: 7
            },
            {
                pic: imageUrl + 'icon_zhanghumingxi.png',
                name: '账户明细',
                url: '../accountdetail/accountdetail',
                id: 8
            },
            {
                pic: imageUrl + 'icon_kefu.png',
                name: '客服',
                url: '../cusser/cusser',
                id: 9
            }
        ],
        is_shop: 0,
        avatar: '',
        nickName: '',
        balance: ''
    },
    goDrawCash() {
        wx.navigateTo({
            url: '../cashdraw/cashdraw?money=' + this.data.balance
        })
    },
    goWhichPage(e) {
        let pageId = e.currentTarget.dataset.id;
        let pageUrl = e.currentTarget.dataset.url;
        if (pageId === 1) {
            wx.switchTab({
                url: pageUrl,
            });
        } else if (pageId === 6) {
            wx.navigateTo({
                url: pageUrl + '?is_shop=' + this.data.is_shop
            });
        } else {
            wx.navigateTo({
                url: pageUrl
            });
        }
    },

    goSetting() {
        wx.navigateTo({
            url: "../setting/setting"
        });
    },
    goMyInfo(e) {
        wx.navigateTo({
            url: '../myInfo/myInfo',
            success: function (res) {
                // 通过eventChannel向被打开页面传送数据
            }
        })
    },
    getNickname() {
        HTTP.getDataAsync('/api/person/index', {})
            .then(res => {
                return new Promise((resolve, reject) => {
                    let userInfo = App.globalData.userInfo;
                    userInfo = { ...userInfo, ...res.data };
                    App.globalData.userInfo = userInfo;
                    resolve(userInfo);
                });
            })
            .then(value => {
                this.setData({
                    avatar: value.avatar,
                    nickName: value.nickname,
                    balance: value.balance,
                    is_shop: value.is_shop
                });
            })
    },




    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            lastres: wx.getStorageSync('lastres')
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let userInfo = App.globalData.userInfo;
        if(Object.keys(userInfo).length==0){
            console.log('应该吧')
            this.setData({
                avatar: '',
                nickName: '',
                balance: '',
                is_shop: ''
            });
            this.onLoad();
        }
        this.getNickname();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})